package com.example2016.cjj.mycustomviewdemo.MyView;

import android.content.Context;
import android.graphics.Point;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by CJJ on 2017/3/3.
 */

public class CustomDragView extends LinearLayout {

    private static final String TAG = "CustomDragView";

    private ViewDragHelper mViewDragHelper;

    private View mNormalView; //普通可以的拖动的view

    private View mAutoBackView; //自动回到原点的view

    private View mLeftEdgeView; //从左边缘开始拖动的view

    private Point mAutoBackOriginPos = new Point(); //记录一开始mAutoBackView的坐标位置


    public CustomDragView(Context context) {
        this(context,null);
    }

    public CustomDragView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomDragView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mViewDragHelper= ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                //禁止mLeftEdgeView直接移动
                if (child == mNormalView || child == mAutoBackView){
                    return true;
                }
                return false;
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                return top;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                return left;
            }


            //手指释放时，回调
            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
               if( releasedChild == mAutoBackView){
                   //回到原来位置
                   mViewDragHelper.settleCapturedViewAt(mAutoBackOriginPos.x,mAutoBackOriginPos.y);
                   invalidate();
               }
            }

            //手指在边缘拖动时，回调
            @Override
            public void onEdgeDragStarted(int edgeFlags, int pointerId) {
                //允许mLeftEdgeView拖动
                mViewDragHelper.captureChildView(mLeftEdgeView,pointerId);
            }
        });

        //指定在左边缘上，来跟踪手指拖动，会去调用onEdgeDragStarted()
        mViewDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);

    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mNormalView = getChildAt(0);
        mAutoBackView = getChildAt(1);
        mLeftEdgeView = getChildAt(2);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        //记录mAutoBackView的原始位置
        mAutoBackOriginPos.x = mAutoBackView.getLeft();
        mAutoBackOriginPos.y = mAutoBackView.getTop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return  mViewDragHelper.shouldInterceptTouchEvent(ev);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        mViewDragHelper.processTouchEvent(event);

        return true;
    }


    @Override
    public void computeScroll() {
        super.computeScroll();
        if(mViewDragHelper.continueSettling(true)){
            invalidate();
        }
    }


}
